package com.ukao.cashregister.dialog;


import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.ukao.cashregister.R;
import com.ukao.cashregister.base.BaseDialogFragment;
import com.ukao.cashregister.bean.BalanceBean;
import com.ukao.cashregister.consts.Constant;
import com.ukao.cashregister.consts.SaveParamets;
import com.ukao.cashregister.eventbus.PayEvent;
import com.ukao.cashregister.retrofit.ApiCallback;
import com.ukao.cashregister.utils.CheckUtils;
import com.ukao.cashregister.utils.T;
import com.ukao.cashregister.utils.Utils;
import com.ukao.cashregister.widget.StateButton;
import com.ukao.cashregister.widget.StateImageView;

import java.util.HashMap;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

import static com.ukao.cashregister.eventbus.PayEvent.Message.PAY_SUCCEED;

/**
 * A simple {@link Fragment} subclass.
 * Use the {@link TradeDialodFragment#newInstance} factory method to
 * create an instance of this fragment.
 */
public class TradeDialodFragment extends BaseDialogFragment {
    // TODO: Rename parameter arguments, choose names that match
    // the fragment initialization parameters, e.g. ARG_ITEM_NUMBER
    private static final String ARG_PARAM1 = "param1";
    private static final String ARG_PARAM2 = "param2";
    @BindView(R.id.close_btn)
    StateImageView closeBtn;
    @BindView(R.id.title)
    TextView title;
    @BindView(R.id.error_state_image)
    ImageView errorStateImage;
    @BindView(R.id.error)
    TextView errortv;
    @BindView(R.id.check_refresh_btn)
    StateButton checkRefreshBtn;
    @BindView(R.id.error_layout)
    LinearLayout errorLayout;
    @BindView(R.id.trade_image)
    ImageView tradeImage;
    @BindView(R.id.trade_state)
    TextView tradeState;
    @BindView(R.id.trade_type)
    TextView tradeType;
    @BindView(R.id.back_home_btn)
    StateButton backHomeBtn;

    @BindView(R.id.look_use_btn)
    StateButton lookUseBtn;
    @BindView(R.id.trade_layout)
    LinearLayout tradeLayout;
    Unbinder unbinder;


    private String mParam1;
    private String mParam2;
    private String error;
    private String tradeSeq;


    public static TradeDialodFragment newInstance(String tradeSeq, String error) {
        TradeDialodFragment fragment = new TradeDialodFragment();
        Bundle args = new Bundle();
        args.putString(ARG_PARAM1, tradeSeq);
        args.putString(ARG_PARAM2, error);
        fragment.setArguments(args);
        return fragment;
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        if (getArguments() != null) {
            tradeSeq = getArguments().getString(ARG_PARAM1);
            error = getArguments().getString(ARG_PARAM2);
        }
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        View view = inflater.inflate(R.layout.fragment_trade_dialod, container, false);
        unbinder = ButterKnife.bind(this, view);
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {
        super.onViewCreated(view, savedInstanceState);
        init();
    }

    private void init() {
    title.setText("");

    }


    @Override
    public void onDestroyView() {
        super.onDestroyView();
        unbinder.unbind();
    }

    @OnClick({R.id.check_refresh_btn, R.id.back_home_btn, R.id.look_use_btn,R.id.close_btn})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case  R.id.close_btn:
                  dismiss();
                break;

            case R.id.check_refresh_btn:

                queryStatusBySeq(tradeSeq);
                break;
            case R.id.back_home_btn:
                if (mOnDialogSureClickListener!=null)
                    dismiss();
                    mOnDialogSureClickListener.onSureItemClick(R.id.back_home_btn);

                break;

            case R.id.look_use_btn:
                if (mOnDialogSureClickListener!=null)
                    dismiss();
                    mOnDialogSureClickListener.onSureItemClick(R.id.look_use_btn);

                break;
        }
    }


    /**
     * 查询交易状态
     * @param seq   交易流水号
     */
    public void queryStatusBySeq (String seq  ) {
        HashMap mHashMap = new HashMap<String, String>();
        mHashMap.put("seq", seq);
        mHashMap.put("accessToken", SaveParamets.getToken(Utils.getContext()));
        addSubscription(apiStores().queryStatusBySeq(Constant.createParameter(mHashMap)),
                new ApiCallback<BalanceBean>() {
                    @Override
                    public void onSuccess(BalanceBean model) {
                        if (model.getHttpCode() == 200) {
                            BalanceBean mData = model.getData();
                            switch (model.getData().getStatus()) {
                                case -1:

                                    break;
                                case 0: /*status -1.交易失败 0.交易中 1.交易成功*/

                                    break;
                                case 1:
                                    fillData(mData);

                                    break;


                            }



                        } else {

                        }

                    }


                    @Override
                    public void onFinish() {
                    }

                });


    }

    public void fillData(BalanceBean mData) {
        //1.微信 4.支付宝
        String  payMethod ="";
        if (mData.getPayMethod()==1){
            payMethod="微信支付";
        }else if (mData.getPayMethod()==4 ){
            payMethod="支付宝支付";
        }
        tradeType.setText(payMethod+","+ CheckUtils.isEmptyNumber(mData.getAmount())+"元");
        tradeImage.setImageResource(R.drawable.use_bing);
        errorLayout.setVisibility(View.GONE);
        tradeLayout.setVisibility(View.VISIBLE);
    }

}
